#!/bin/bash

###############################################
#
#   https://chatserverinstall.googlecode.com/svn/trunk/centos6.2.sh
#   Installation Script to Install chatserver, libevent2, Go, redis, lua and other required
#   Supporting software on Centos 6.2
#   Copyright (C) 2012, ku-xing.com <zhangeihao@ku-xing.com>
#  
###############################################

VERSION="0.2"

# install confirm
cat <<EOT
+----------------------------------------------------------------------------+
| This Script will install and create base line configs for Chat Server.     |
|                                                                            |
| It is designed to run on a Centos6 Minimal Install. EPEL will also be      |
| temporarily Enabled to get a few packages not in the main Centos           |
| Repositories.                                                              |
|                                                                            |
| As with anything you will want to review the configs after the installer   |
| to make sure they are what you want.                                       |
|                                                                            |
| Coperight (C) 2012, ku-xing.com <zhangpeihao@ku-xing.com>                  |
+----------------------------------------------------------------------------|
EOT

echo "VERSION: $VERSION"
read -r -p "Are you sure? [Y/n] " response
if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]
then
    echo "Here we go..."
else
    echo "Aborting"
    exit
fi

#=============================================================
# Log functions
export INSTALL_LOG_FILE=$(pwd)/install.log
function if_error
{
    if [ $1 -ne 0 ]
    then 
        echo -e "\033[31mfailed!error code:$1\033[0m"
        echo "failed!error code:$1" >> $INSTALL_LOG_FILE
        exit $1
    else
        echo -e "\033[32msuccess!\033[0m"
    fi
}
function dump
{
    echo -e "\033[44m$1\033[0m"
    echo -n "$1" >> $INSTALL_LOG_FILE
}
#==============================================================

#Global value
CPU_NUMBER=$(cat /proc/cpuinfo |grep processor |wc -l)
INSTALL_ROOT=/downloads

#Get the machine type x86_64
dump "Get the machine type x86_64"
MACHINE_TYPE=$(uname -m)
if [ $MACHINE_TYPE != "x86_64" ]
then
    dump "Chat server must run on x86_64 architecture"
    if_error -1
else
    if_error 0
fi

# Update system and then install all required modules
dump "Update system and then install all required modules"
yum update -y
yum -y install autoconf automake gcc-c++ git-core libtool make ncurses-devel pkgconfig openssl-devel zlib zlib-devel bzip2 subversion nohup screen ntp cmake readline-devel chkconfig
if_error $?

# Make install root folder
dump "Make install root folder"
mkdir -p $INSTALL_ROOT
if_error $?

# Update time
dump "Update time"
rm /etc/localtime
if_error $?
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
if_error $?
ntpdate pool.ntp.org
if_error $?
service ntpd start
if_error $?
chkconfig ntpd on
if_error $?

# Install libevent2
cd $INSTALL_ROOT
dump "Install libevent2"
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
if_error $?
tar zxf libevent-2.0.21-stable.tar.gz
if_error $?
cd libevent-2.0.21-stable
./configure
if_error $?
make -j $CPU_NUMBER && make install
if_error $?

# Install Go
cd $INSTALL_ROOT
dump "Install go1.0.3"
wget http://go.googlecode.com/files/go1.0.3.linux-amd64.tar.gz
if_error $?
rm -r /usr/local/go
tar -C /usr/local -xzf go1.0.3.linux-amd64.tar.gz
if_error $?
echo "export PATH=\$PATH:/usr/local/go/bin" >> /etc/profile
if_error $?
export PATH=$PATH:/usr/local/bin:/usr/local/go/bin
if_error $?

# Install redis
cd $INSTALL_ROOT
dump "Install redis-2.6.5"
wget http://redis.googlecode.com/files/redis-2.6.5.tar.gz
if_error $?
tar xzf redis-2.6.5.tar.gz
if_error $?
cd redis-2.6.5
if_error $?
make -j $CPU_NUMBER
if_error $?
cp ./src/redis-server /usr/local/bin/redis-server
if_error $?
cp ./src/redis-cli /usr/local/bin/redis-cli
if_error $?
useradd -M -d /var/redis redis
if_error $?
mkdir /etc/redis
if_error $?
mkdir -p /var/redis/6379
if_error $?
mkdir -p /var/run/redis
if_error $?
mkdir -p /var/log/redis
if_error $?
wget http://chatserverinstall.googlecode.com/svn/trunk/redis-6379
if_error $?
chmod +x ./redis-6379
cp ./redis-6379 /etc/init.d/redis-6379
if_error $?
wget http://chatserverinstall.googlecode.com/svn/trunk/6379.conf
if_error $?
mv 6379.conf /etc/redis/6379.conf
if_error $?
chown -R redis:redis /etc/redis
if_error $?
chown -R redis:redis /var/redis
if_error $?
chown -R redis:redis /var/run/redis
if_error $?
chown -R redis:redis /var/log/redis
if_error $?
chkconfig --add redis-6379
if_error $?
/etc/init.d/redis-6379 start
if_error $?

# Install gflags-2.0
cd $INSTALL_ROOT
dump "Install gflags-2.0"
wget http://gflags.googlecode.com/files/gflags-2.0.tar.gz
if_error $?
tar xzf gflags-2.0.tar.gz
if_error $?
cd gflags-2.0
if_error $?
./configure
if_error $?
make -j $CPU_NUMBER && make install
if_error $?
ldconfig

# Install glog-0.3.2
cd $INSTALL_ROOT
dump "Install glog-0.3.2"
wget http://google-glog.googlecode.com/files/glog-0.3.2.tar.gz
if_error $?
tar xzf glog-0.3.2.tar.gz
if_error $?
cd glog-0.3.2
if_error $?
./configure
if_error $?
make -j $CPU_NUMBER && make install
if_error $?
ldconfig

# Install libunwind-0.99-beta
cd $INSTALL_ROOT
dump "Install libunwind-0.99-beta"
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-beta.tar.gz
if_error $?
tar xzf libunwind-0.99-beta.tar.gz
if_error $?
cd libunwind-0.99-beta
if_error $?
./configure
if_error $?
make -j $CPU_NUMBER && make install
if_error $?
ldconfig

# Install gperftools-2.0
cd $INSTALL_ROOT
dump "Install gperftools-2.0"
wget http://gperftools.googlecode.com/files/gperftools-2.0.tar.gz
if_error $?
tar xzf gperftools-2.0.tar.gz
if_error $?
cd gperftools-2.0
if_error $?
./configure
if_error $?
make -j $CPU_NUMBER && make install
if_error $?
ldconfig

# Install lua-5.1.5
cd $INSTALL_ROOT
dump "Install lua-5.1.5"
wget http://www.lua.org/ftp/lua-5.1.5.tar.gz
if_error $?
tar xzf lua-5.1.5.tar.gz
if_error $?
cd lua-5.1.5
if_error $?
make linux && make INSTALL_TOP=/usr INSTALL_LIB=/usr/lib64 install
if_error $?
cd /usr/include
if_error $?
mkdir lua5.1
if_error $?
mv lua*.h ./lua5.1/
if_error $?
mv lauxlib.h ./lua5.1/
if_error $?
cd /usr/lib64
ln -s ./liblua.a ./liblua5.1.a
ldconfig

# Install lua-cjson
cd $INSTALL_ROOT
dump "Install lua-cjson"
wget http://www.kyne.com.au/%7Emark/software/download/lua-cjson-2.1.0.tar.gz
if_error $?
tar xzf lua-cjson-2.1.0.tar.gz
if_error $?
cd lua-cjson-2.1.0
if_error $?
mkdir build
cd build
cmake ..
if_error $?
make -j $CPU_NUMBER && make install
if_error $?
ldconfig


